#  Copyright (c) 2023. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
#  Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
#  Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
#  Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
#  Vestibulum commodo. Ut rhoncus gravida arcu.

# coding:utf8
from log import logger

from project import NetWorkCard, Card, set_pc_ips


class ProxyNetWorkCard:
    """
   代理NetWorkCard 连接 视图,作为中间件
    
    """
    
    def __init__(self):
        self.NetWorkCard = NetWorkCard()
        self.card: Card = self.NetWorkCard.get_card_from_name()
        if self.card is None:
            self.card = Card()
    
    def switch_current_card(self, card_name):
        """
        切换网卡
        :return True or None
        
        """
        card = self.NetWorkCard.get_card_from_name(card_name)
        if card:
            self.card = card
            logger.debug("切换网卡成功, 当前网卡名称: {}".format(card_name))
            return True
    
    def get_cardname_list(self):
        """
        获取网卡的名称列表
        :return  [card_name...]
        """
        cards_name_list = list(self.NetWorkCard.get_card_names())
        curr_card_name = self.card.name
        if curr_card_name in cards_name_list:
            cards_name_list.remove(curr_card_name)
        
        # 放在列表的首位
        cards_name_list.insert(0, curr_card_name)
        
        return cards_name_list
    
    def add_and_set_pc_ip(self, ip):
        """
        向本身IP添加一个IP
        """
        ips = self.card.ips()
        ips.append(ip)
        
        logger.debug("当前Card要增加的IP是:{}".format(ip))
        rev = set_pc_ips(ips, self.card)
        return rev